package edu.gatech.cs3240;

import java.util.ArrayList;
import java.util.Hashtable;

import edu.gatech.cs3240.NFA.NFA;

/**
 * This is the initial parser thread, which spawns and 'owns' the threads of
 * NewParser.  It also acts as an interface to the rest of the application.
 * @author VRamachandran
 */
public class NewStartParser extends NewParser {
	
	private NFA nfa;
	private String workingString;
	private NewParser child;
	private boolean success;
	private boolean finished;
	private static final Hashtable<edu.gatech.cs3240.NFA.State, 
		ArrayList<String>> visited = new Hashtable
			<edu.gatech.cs3240.NFA.State, ArrayList<String>>();
	
	public NewStartParser(NFA nfa, String workingString) {
		super(-1, null, null, workingString);
		this.nfa = nfa;
		this.workingString = workingString;
		this.finished = false;
		this.success = false;
		NewParser.nextId = 1;
		this.start();
	}
	
	public static void visit(edu.gatech.cs3240.NFA.State s, String st) {
		if(visited.contains(s)) {
			visited.get(s).add(st);
		} else {
			ArrayList<String> a = new ArrayList<String>();
			a.add(st);
			visited.put(s, a);
		}
	}
	
	public static boolean hasBeenVisited(edu.gatech.cs3240.NFA.State s, String st) {
		if(visited.containsKey(s))
			if(visited.get(s).contains(st))
				return true;
		return false;
	}
	
	public void run() {
		super.setStatus(Status.waiting);
		child = new NewParser(0, this, nfa.getStartState(), workingString);
		child.start();
	}
	
	public boolean isFinished() {
		return finished;
	}
	
	public boolean success() {
		return success;
	}
	
	public synchronized void reportSuccess(NewParser p) {
		success = true;
		finished = true;
	}
	
	public synchronized void reportFailure(NewParser p) {
		finished = true;
	}
	
	public static void reset() {
		visited.clear();
	}
}
